Ext.namespace("NewCustomer");

NewCustomer.Edit = function(p) {
	this.parentWin=p;	
	this.winMsg=null;
	this.temp=0;
	
	this.txtRemind=new Ext.form.Hidden({name:'remind'});
	this.hdSuperviseId = new Ext.form.Hidden({name:'superviseId'});
	this.txtSuperviseTitle = new Ext.form.TextField({
		fieldLabel: '问题标题',
		name:'superviseTitle',		
		maxLength:100,
		width: 380
	});
	
	this.txtManagerId=new Ext.form.Hidden({name:'managerId'});
	this.txtManagerName = new Ext.form.Hidden({name:'managerName'});
	this.txtReportDept=new Ext.form.Hidden({name:'reportDept'});
	this.txtReportDeptName = new Ext.form.TextField({
		width: 150,
		fieldLabel: '责任部门',
		name:'reportDeptName',
		readOnly: true
	});
	
	this.txtSuperviseContent = new Ext.form.TextArea({
		fieldLabel: '资料描述',
		name:'superviseContent',
		maxLength:1000,
		blankText:'请输入资料描述',
		allowBlank:false,
		height:60,
		width: 380
	});
	
	this.typeStore  = new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.FILE_TYPE2
    });
	this.txtIssueType = new Ext.form.ComboBox({
		width: 150,
		fieldLabel: '资料类别',
		name:'issueType',
		store: this.typeStore,
        valueField: 'value',
        displayField: 'name',
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	this.txtIssueType.on('select', this.changeType, this);	
	this.dtIssueDate = new Ext.form.DateField({
    	fieldLabel: '上传时间',
    	name:'issueDate',
		width: 150,
		format: 'Y-m-d'
	});
	this.typeLebel  = new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.FILE_LEVEL2
    });
	this.txtInstancyLevel = new Ext.form.ComboBox({
		width: 150,
		fieldLabel: '重要程度',
		name:'instancyLevel',
		store: this.typeLebel,
		value:"0",	
        valueField: 'value',
        displayField: 'name',
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	this.form = new Ext.FormPanel({
		bodyStyle: 'padding: 5px 5px 0',
        labelWidth: 70,
        labelAlign: 'right',
        items: [{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .50, items: [this.txtIssueType]},
	        	         { columnWidth: .50, items: [this.txtReportDeptName]}]
	    				},{ layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .50, items: [this.txtInstancyLevel]},
	        	         { columnWidth: .50, items: [this.dtIssueDate]}]
	    	},this.txtSuperviseContent,this.hdSuperviseId,this.txtManagerId,
	    		this.txtReportDept,this.txtRemind
	    	]
    });
    this.fileForm = new Ext.FormPanel({
        bodyStyle: 'padding: 5px 5px 0',
        labelWidth: 70,
        labelAlign: 'right',
        fileUpload: true, 
        items: []
	});
    this.btnSave = new Ext.Button({
    	text:'保存',
    	handler:this.onSave,
    	scope: this
    });
    this.btnSubmit = new Ext.Button({
    	text:'提交',
    	handler:this.onSubmit,
    	scope: this
    });
	NewCustomer.Edit.superclass.constructor.call(this, {
		title: '创建附件下载',
        autoHeight: false,
        width: 500,
        y: 20,
        resizable: false,
        plain: true,
        modal: true,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.form,this.fileForm],
        buttons: [this.btnSave,
        this.btnSubmit,
        {
			text: '添加附件',
			iconCls: 'icon-new',
			handler: this.addFile,
			scope: this
        },{
            text: '关闭',
            scope: this,
            handler: function() {
	        	this.hide();
        	}
        }],
        buttonAlign:'center'
	});
};
Ext.extend(NewCustomer.Edit, Ext.Window, {
	initWin:function(data){
		//alert(Ext.util.JSON.encode(data));
		if(data){
			this.form.getForm().setValues(data);
			this.initFile(data);
		}else{
			this.form.getForm().reset();
			this.fileForm.removeAll(true);
			this.dtIssueDate.setValue(new Date());
		}			
	},
	initFile:function(o){
		this.fileForm.removeAll(true);
		var conn = new Ext.data.Connection({
			url:'../superviseIssue/searchUpLoadFiles.form?timesnap=' + Date.parse(new Date()),
			method:'post'
		})
		Utils.request(conn,{
			params:{superviseId:o.superviseId},
			scope:this,
			success:function(data){
				var files=data.result;
				for(var i=0;i<files.length;i++){
					this.scope.addFile(files[i]);
				}
			}
		});
	},
	addFile:function(o){
		this.temp+=1;
		var fname="fileName"+this.temp;
		var hname="hideFileId"+this.temp;
		var fileText=(o.ACCESSORY_NAME==null?"请选择要上传附件":o.ACCESSORY_NAME);
		var fileField=new Ext.form.FileUploadField({
	    	fieldLabel: '上传附件',
			emptyText: fileText,
			width : 220,
			name : fname,
			buttonText: '',
			buttonCfg: {
	            iconCls: 'icon-compress'
	        }
	    });
	    var fileDel=new Ext.Button({
	    	text: '清除',
	    	iconCls: 'icon-remove',
	    	scope: this,
	    	handler: this.delFile
	    });
	    if(o.ACCESSORY_ID){
		    var hdFile=new Ext.form.Hidden({
		    	name:hname,
		    	value:o.ACCESSORY_ID
		    });
		    var href='../superviseIssue/downloadFile.form?fileId='+o.ACCESSORY_ID+'&timesnap=' + Date.parse(new Date());
			var lbLink=new Ext.form.Label({
				html:'<a href="'+href+'">'+o.ACCESSORY_NAME+'</>',
				width:280
			});			
			var item={ layout: 'column', border: false, 
					defaults: {layout: 'form', border: false},
		        	items: [ { columnWidth: .86, items: [fileField]},
		        	         { columnWidth: .14, items: [fileDel] },
		        	         { columnWidth: 1, items: [lbLink] },
		        	         hdFile]
		    };
	    }else{
	    	var item={ layout: 'column', border: false, 
					defaults: {layout: 'form', border: false},
		        	items: [ { columnWidth: .86, items: [fileField]},
		        	         { columnWidth: .14, items: [fileDel] }
		        	       ]
		    };
	    }   	
		this.fileForm.add(item);
	    this.fileForm.doLayout();
	},
	delFile:function(btn,event){
		var item=btn.ownerCt.ownerCt;
		this.fileForm.remove(item,true);
	    this.fileForm.doLayout();
	},
	changeType:function(combo,record,index){
		var v=this.txtIssueType.getValue();
		if(v=='7'||v=='8'||v=='9'){
			this.txtReportDept.setValue('2177');
			this.txtReportDeptName.setValue('综合部');
			this.txtManagerId.setValue('20155');
			this.txtManagerName.setValue('刘琪');
		}else{
			this.txtReportDept.setValue('2178');
			this.txtReportDeptName.setValue('市场部');
			this.txtManagerId.setValue('23421');
			this.txtManagerName.setValue('郭超');
		}
	},
	onSave: function() {
		this.saveIssue('0');
	},
	onSubmit: function() {
		scope=this;
		if(this.winMsg==null)
			this.winMsg=new Message.Msg(function(){
				scope.txtRemind.setValue(scope.winMsg.txtRemind);
				scope.submitIssue();
			},'提交后将不可修改，你确实要提交吗？');
		this.winMsg.show();
	},
	submitIssue:function(){
		this.saveIssue('1');
	},
	saveIssue: function(state) {
		if(!this.form.getForm().isValid()){
			Utils.info2("有必填项没有填写，请检查！",180);
			return;
		}  		
  		var url='../superviseIssue/saveIssue.form?timesnap=' + Date.parse(new Date());
		var param=this.form.getForm().getFieldValues();
  		Utils.formatDate(param,'issueDate');
  		param['superviseState']=state;
  		//alert(Ext.util.JSON.encode(param));	
  		/*var conn = new Ext.data.Connection({
  			url:url,
  			method:'post'
  		})
  		Utils.request(conn,{
  			params:param,
  			scope:this,
  			success:function(data){
  				Utils.info2();
				this.scope.hide();
				this.scope.parentWin.queryIssue();
  			}
  		});*/  		
		scope=this;
		this.fileForm.getForm().submit({
			waitTitle : '系统提示',
			waitMsg : '数据提交中，请稍候...',
			url : url,
			params:param,
			method : 'POST',			
			success : function(fp, o){
				Utils.info2();
  				scope.hide();			
				scope.parentWin.queryIssue();
			},
			failure : function(fp, o){
				alert('error');
			}
		});  		
	}
});
